<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('厂家服务费管理列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>厂家名称：</label>
                                <input type="text" name="factoryName"/>
                            </li>
                            <li>
                                <label style="width: 120px;">核算期间年份：</label>
                                <input type="text" name="periodYear"/>
                            </li>
                            <li>
                                <label style="width: 120px;">核算期间月份：</label>
                                <input type="text" name="periodMonth"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <!--<div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="zsyh:factoryPay:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="zsyh:factoryPay:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="zsyh:factoryPay:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="zsyh:factoryPay:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>-->
            <div class="col-sm-12 select-table table-bordered">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('zsyh:factoryPay:edit')}]];
        var removeFlag = [[${@permission.hasPermi('zsyh:factoryPay:remove')}]];
        var relAccountDatas = [[${@dict.getType('zsyh_account')}]];
        var payMethodDatas = [[${@dict.getType('pay_type')}]];
        var prefix = ctx + "zsyh/factoryPay";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                detailView: true,
                onExpandRow : function(index, row, $detail) {
                    initChildTable(index, row, $detail);
                },
                modalName: "厂家服务费管理",
                columns: [{
                    field : 'Number',
                    title : '序号',
                    align: 'center',
                    width: 20,
                    formatter : function(value, row, index) {
                        var pageSize = $('#bootstrap-table').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                        var pageNumber = $('#bootstrap-table').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                        return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                    }
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'factoryName',
                    align: 'center',
                    title: '厂家名称'
                },
                {
                    field: 'payBegin',
                    align: 'center',
                    title: '期初应付服务费'
                },
                {
                    field: 'payOn',
                    align: 'center',
                    title: '本月新增服务费'
                },
                {
                    field: 'payOff',
                    align: 'center',
                    title: '本月已付服务费'
                },
                {
                    field: 'payEnd',
                    align: 'center',
                    title: '本月剩余服务费'
                },
                {
                    field: 'periodYear',
                    align: 'center',
                    title: '核算期间年份'
                },
                {
                    field: 'periodMonth',
                    align: 'center',
                    title: '核算期间月份'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="viewDetail(\'' + row.factoryId + '\',\''+row.periodYear+'\',\''+row.periodMonth+'\')"><i class="fa fa-edit"></i>查看费用详情</a> ');
                        var myDate = new Date();
                        var currentYear = myDate.getFullYear();
                        var currentMonth = myDate.getMonth()+1;
                        var createDate = new Date(row.createTime);
                        if(row.payEnd !=0 && (currentYear == row.periodYear && currentMonth == row.periodMonth)){
                            actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="addPayRel(\'' + row.factoryId + '\',\''+row.id+'\')"><i class="fa fa-plus"></i>添加结算信息</a> ');
                        }

                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        initChildTable = function(index, row, $detail) {
            var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
            $(childTable).bootstrapTable({
                url: prefix + "/listChild",
                modalName: "服务费结账详细",
                method: 'post',
                sidePagination: "server",
                contentType: "application/x-www-form-urlencoded",
                queryParams: {
                    mid: row.id
                },
                columns: [{
                    field : 'Number',
                    title : '序号',
                    align: 'center',
                    width: 30,
                    formatter : function(value, row, index) {
                        var pageSize = $('#bootstrap-table').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                        var pageNumber = $('#bootstrap-table').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                        return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                    }
                },
                    {
                        field: 'id',
                        title: '主键',
                        align: 'center',
                        visible: false
                    },
                    {
                        field: 'paidMoney',
                        align: 'center',
                        title: '本次支付金额'
                    },
                    {
                        field: 'discountMoney',
                        align: 'center',
                        title: '优惠金额'
                    },
                    {
                        field: 'remainMoney',
                        align: 'center',
                        title: '合计结算金额'
                    },
                    {
                        field: 'relAccount',
                        align: 'center',
                        title: '入账账户',
                        formatter: function(value, row, index) {
                            return $.table.selectDictLabel(relAccountDatas, value);
                        }
                    },
                    {
                        field: 'payMethod',
                        align: 'center',
                        title: '付款方式',
                        formatter: function(value, row, index) {
                            return $.table.selectDictLabel(payMethodDatas, value);
                        }
                    },
                    {
                        field: 'operateId',
                        align: 'center',
                        title: '操作人'
                    },
                    {
                        field: 'createTime',
                        align: 'center',
                        title: '操作时间'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                            var myDate = new Date();
                            var currentYear = myDate.getFullYear();
                            var currentMonth = myDate.getMonth()+1;
                            var createDate = new Date(row.createTime);
                            if(createDate.getFullYear() == currentYear && (createDate.getMonth()+1) == currentMonth){
                                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="editPayRel(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                                actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="removePayRel(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                            }
                            return actions.join('');
                        }
                    }
                ]
            });
        };

        function viewDetail(factoryId,periodYear,periodMonth) {
            debugger;
            var url = ctx+"zsyh/factoryPay/viewDetail?factoryId="+factoryId+"&periodYear="+periodYear+"&periodMonth="+periodMonth;
            layer.open({
                type: 2,
                area: ['800px', '500px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: "查看费用详情",
                content:  url,
                closeBtn: 1,
                // 弹层外区域关闭
                shadeClose: true,
                end: function(){
                    //location.reload();
                },
                cancel: function(index) {
                    //location.reload();
                }
            });
        }

        function addPayRel(factoryId,id) {
            var url = ctx + 'zsyh/factoryPayRel/addPayRel?id='+id;
            $.modal.open("开始核算", url, '1000', '500');
        }

        function editPayRel(id) {
            var url = ctx + "zsyh/factoryPayRel/edit/"+id;
            $.modal.open('修改结算记录', url, '900', '500');
        }

        function removePayRel(id) {
            $.modal.confirm("确认要删除选中的数据吗?", function() {
                var data = { "ids": id};
                $.operate.submit(ctx + "zsyh/factoryPayRel/remove/", "post", "json", data);
            });
        }

    </script>
</body>
</html>